//
//  SHRWSHCGViewController.m
//  VmiSJ
//
//  Created by 汪的一声就哭了！ on 2017/1/6.
//  Copyright © 2017年 汪的一声就哭了！. All rights reserved.
//
//容器标题高度
#define TitleHeight self.fontSize*3
#import "SHRWSHCGViewController.h"

@interface SHRWSHCGViewController ()<UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation SHRWSHCGViewController
{
    NSMutableArray *_listArray;
    NSInteger page;
}
- (void)setState:(NSString *)state
{
    _state = state;
    [self obtainTaskData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self obtainTaskData];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 4, WIDTH, HEIGHT-64-TitleHeight) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.tableView registerNib:[UINib nibWithNibName:@"SHRWDSHTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell1"];
    [self.tableView registerNib:[UINib nibWithNibName:@"SHRWNoImgTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell2"];
    [self addRefresh];
}
- (void)addRefresh {
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(obtain)];
    
    // 自动改变透明度
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreTopics)];
}
- (void)timerAction:(NSTimer *)timer
{
    [self.tableView numberOfRowsInSection:0];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _listArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SHRWModel *model = _listArray[indexPath.row];
    if (model.images.count == 0) {
        SHRWNoImgTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
        cell.userLabel.text = [NSString stringWithFormat:@"用户：%@",model.user_phone];
        cell.detailLabel.text = [model.t_message componentsJoinedByString:@"\n"];
        cell.timeLabel.text = model.submit_time;
        cell.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.clipsToBounds = YES;
        return cell;
    }
    SHRWDSHTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
    cell.collectionView.tag = 100+indexPath.row;
    cell.collectionView.delegate = self;
    cell.collectionView.dataSource = self;
    [cell.collectionView reloadData];
    [cell.collectionView registerNib:[UINib nibWithNibName:@"SHRWDSHCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"collection"];
    cell.flowLayout.itemSize = CGSizeMake(70, 70);
    cell.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    cell.flowLayout.minimumInteritemSpacing = 8;
    cell.flowLayout.sectionInset = UIEdgeInsetsMake(0, 5, 0, 5);
    
    cell.userLabel.text = [NSString stringWithFormat:@"用户：%@",model.user_phone];
    cell.detailLabel.text = [model.t_message componentsJoinedByString:@"\n"];
    cell.timeLabel.text = model.submit_time;
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.clipsToBounds = YES;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SHRWModel *model = _listArray[indexPath.row];
    NSString *str = [model.t_message componentsJoinedByString:@"\n"];
    CGRect rect = [str boundingRectWithSize:CGSizeMake(WIDTH-98, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil];
    CGFloat height = rect.size.height;
    if (rect.size.height < 18) {
        height = 18;
    }
    if (model.images.count == 0) {
        return 40+height;
    }
    return 126+height;
}
#pragma mark -------------- collectionView
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SHRWDSHCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collection" forIndexPath:indexPath];
    SHRWModel *model = _listArray[collectionView.tag-100];
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:model.images[indexPath.item]] placeholderImage:[UIImage imageNamed:@"占位图"]];
    return cell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    SHRWModel *model = _listArray[collectionView.tag-100];
    return model.images.count;
}
#pragma mark -------------- 点击图片放大
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    SHRWModel *model = _listArray[collectionView.tag-100];
    [self.delegate SHRWSHCGViewControllerDidSelectWithInt:indexPath.row andImgArray:model.images];
}
- (void)obtain {
    [self obtainTaskData];
}
#pragma mark -------------- 审核成功任务
- (void)obtainTaskData
{
    page = 1;
    [NetRequiest handleDataWithString:[NSString stringWithFormat:@"%@",HTTPLOAD] dictionary:@{@"v1":@"reviewcheck",@"type":@"2",@"task_id":self.task_id,@"page":[NSString stringWithFormat:@"%ld",page]} HUD:NO block:^(id responseObject) {
        [self.tableView.mj_header endRefreshing];
        if ([responseObject[@"code"] isEqual:@200]) {
            _listArray = [NSMutableArray array];
            for (NSDictionary *dic in responseObject[@"result"]) {
                SHRWModel *model = [[SHRWModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [_listArray addObject:model];
            }
        }
        page++;
        [self.tableView reloadData];
        [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
    }];
}
- (void)loadMoreTopics
{
    [NetRequiest handleDataWithString:[NSString stringWithFormat:@"%@",HTTPLOAD] dictionary:@{@"v1":@"reviewcheck",@"type":@"2",@"task_id":self.task_id,@"page":[NSString stringWithFormat:@"%ld",page]} HUD:NO block:^(id responseObject) {
        [self.tableView.mj_footer endRefreshing];
        if ([responseObject[@"code"] isEqual:@200]) {
            for (NSDictionary *dic in responseObject[@"result"]) {
                SHRWModel *model = [[SHRWModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [_listArray addObject:model];
            }
        }
        page++;
        [self.tableView reloadData];
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
